package com.kehao.accountbook;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.google.gson.Gson;
import com.kehao.accountbook.network.Network;
import com.kehao.accountbook.network.TwoCaseCallBack;
import com.kehao.accountbook.pojo.User;

public class ChangePasswordActivity extends AppCompatActivity {
    private static final int UPDATA_VIEW=1;
    private EditText et_password1;
    private EditText et_password2;
    private ProgressBar progressBar;
    private String password1 = null;
    private String password2 = null;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);
        et_password1 = findViewById(R.id.et_password1);
        et_password2 =findViewById(R.id.et_password2);
        progressBar = findViewById(R.id.progressBar);
        progressBar.setVisibility(View.INVISIBLE);
        handler = new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                switch (msg.what){
                    case UPDATA_VIEW:
                        String text = msg.getData().getString("fail");
                        Toast.makeText(ChangePasswordActivity.this, text, Toast.LENGTH_SHORT).show();
                        progressBar.setVisibility(View.INVISIBLE);
                        break;
                }
            }
        };
    }


    public void onClickSubmit(View view) {
        closeKeyboard();
        password1 = et_password1.getText().toString();
        password2 = et_password2.getText().toString();
        if(password1.equals("")||password2.equals("")){
            Toast.makeText(ChangePasswordActivity.this, "请在文本框内填写密码", Toast.LENGTH_SHORT).show();
            return;
        }else if (!password1.equals(password2)){
            Toast.makeText(ChangePasswordActivity.this, "两次输入的密码不相同", Toast.LENGTH_SHORT).show();
            return;
        }else {
            final String username = getAccountApplication().getUsername();
            User user = new User(username,password1);
            String json =new Gson().toJson(user);
            Network network = new Network(new TwoCaseCallBack() {
                @Override
                public void success(String string) {
                    if(string.equals("ok")){
                        Intent intent = new Intent();
                        Bundle bundle = new Bundle();
                        bundle.putString("username",username);
                        bundle.putString("password",password1);
                        intent.putExtras(bundle);
                        setResult(RESULT_OK,intent);
                        ChangePasswordActivity.this.finish();
                    }else{
                        Message message = new Message();
                        message.what = UPDATA_VIEW;
                        Bundle bundle = new Bundle();
                        bundle.putString("fail","其他错误");
                        message.setData(bundle);
                        handler.sendMessage(message);
                    }

                }

                @Override
                public void fail() {
                    Message message = new Message();
                    message.what = UPDATA_VIEW;
                    Bundle bundle = new Bundle();
                    bundle.putString("fail","网络异常");
                    message.setData(bundle);
                    handler.sendMessage(message);
                }
            },json,Network.CHANGE_PASSWORD_URL,(AccountApplication) getApplication());
            network.send();
            progressBar.setVisibility(View.VISIBLE);
        }
    }

    private void closeKeyboard() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
    }

    private AccountApplication getAccountApplication(){
        return (AccountApplication)getApplication();
    }
}
